﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Addin665.Contract;
using System.Collections.ObjectModel;

namespace Addin665.Commands {
    /// <summary>
    /// command 管理器
    /// </summary>
    class CommandManager {
        ObservableCollection<ICommand> _Commands = new ObservableCollection<ICommand>();

        internal ObservableCollection<ICommand> Commands {
            get { return _Commands; }
            set { _Commands = value; }
        }

        public CommandManager() {
        }

        public void CreateCommands(object hook) {
            _Commands.ToList().ForEach(a => a.OnCreate(hook));
        }

        public ICommand LookUp(string cmdName) {
            foreach (var item in Commands) {
                if (item.Name == cmdName) {
                    return item;
                }
            }
            return null;
        }

        public void ActiveCommand(string cmdName) {
            ICommand cmd = this.LookUp(cmdName);
            this.ActiveCommand(cmd);
        }

        public void ActiveCommand(ICommand cmd) {
            try {
                cmd.OnClick(); 
            }
            catch (Exception ex) {


            }
        }
    }
}
